package com.nest.WorkNest.act;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.nest.WorkNest.R;
import com.nest.WorkNest.adapter.AdPageAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by chenchuan on 2015/9/7 0007.
 */
public class IntroductionAct extends BaseActivity {
    @Bind(R.id.img_introduct_next)
    ImageView imgIntroductNext;
    @Bind(R.id.img_introduct_last)
    ImageView imgIntroductLast;
    private ViewPager adViewPager;
    private LinearLayout pagerLayout;
    private List<View> pageViews;
    private ImageView[] imageViews;
    private ImageView imageView;
    private AdPageAdapter adapter;
    private AtomicInteger atomicInteger = new AtomicInteger(0);
    private Context context = IntroductionAct.this;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.act_introduction);
        ButterKnife.bind(this);
        initViewPager();
        initBar();
        actionbar_side_name.setVisibility(View.VISIBLE);
        actionbar_side_name.setText("服装定制流程");
        ll_iv_actionbar_side_left.setVisibility(View.VISIBLE);
        back();
    }

    private void initViewPager() {

        // 从布局文件中获取ViewPager父容器
        pagerLayout = (LinearLayout) findViewById(R.id.ll_introduction_content);
        // 创建ViewPager
        adViewPager = new ViewPager(this);

        // 获取屏幕像素相关信息
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        // 根据屏幕信息设置ViewPager广告容器的宽高
        adViewPager.setLayoutParams(new ViewGroup.LayoutParams(dm.widthPixels,
                dm.heightPixels));

        // 将ViewPager容器设置到布局文件父容器中
        pagerLayout.addView(adViewPager);

        initPageAdapter();

//        initCirclePoint();

        adViewPager.setAdapter(adapter);
        adViewPager.setOnPageChangeListener(new AdPageChangeListener());

    }

    private void initPageAdapter() {
        pageViews = new ArrayList<View>();

        ImageView img1 = new ImageView(this);
        img1.setBackgroundResource(R.drawable.img_phonemade);
        img1.setScaleType(ImageView.ScaleType.FIT_CENTER);
        pageViews.add(img1);

        ImageView img2 = new ImageView(this);
        img2.setBackgroundResource(R.drawable.img_phonesearch);
        img2.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        pageViews.add(img2);

        adapter = new AdPageAdapter(pageViews);
    }

    private void initCirclePoint() {
        ViewGroup group = (ViewGroup) findViewById(R.id.ll_navigation_viewGroup);
        imageViews = new ImageView[pageViews.size()];
        // 广告栏的小圆点图标
        for (int i = 0; i < pageViews.size(); i++) {
            // 创建一个ImageView, 并设置宽高. 将该对象放入到数组中
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    30, 30);
            imageView = new ImageView(this);
            params.setMargins(10, 0, 10, 0);
            imageView.setLayoutParams(params);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageViews[i] = imageView;

            // 初始值, 默认第0个选中
            if (i == 0) {
                imageViews[i].setBackgroundResource(R.drawable.point_focused);
            } else {
                imageViews[i].setBackgroundResource(R.drawable.point_unfocused);
            }
            // 将小圆点放入到布局中
            group.addView(imageViews[i]);
        }
    }

    /**
     * ViewPager 页面改变监听器
     */
    private class AdPageChangeListener implements ViewPager.OnPageChangeListener {

        /**
         * 页面滚动状态发生改变的时候触发
         */
        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        /**
         * 页面滚动的时候触发
         */
        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        /**
         * 页面选中的时候触发
         */
        @Override
        public void onPageSelected(int currentIndex) {
            // 获取当前显示的页面是哪个页面
            atomicInteger.getAndSet(currentIndex);
            // 重新设置原点布局集合
//            for (int i = 0; i < imageViews.length; i++) {
//                imageViews[currentIndex]
//                        .setBackgroundResource(R.drawable.point_focused);
//                if (currentIndex != i) {
//                    imageViews[i]
//                            .setBackgroundResource(R.drawable.point_unfocused);
//                }
//            }
            if (currentIndex == 0) {
                actionbar_side_name.setText("服装定制流程");
                imgIntroductLast.setVisibility(View.GONE);
                imgIntroductNext.setVisibility(View.VISIBLE);
            } else if (currentIndex == 1) {
                actionbar_side_name.setText("找款流程");
                imgIntroductLast.setVisibility(View.VISIBLE);
                imgIntroductNext.setVisibility(View.GONE);
            }
        }

    }
}
